<template>
  <div id="part15">需要打开控制台</div>
</template>
<script>
  export default {
    name: "part15",
    components: {},
    data() {
      return {};
    },
    methods: {},
    created: () => {
      async function timeout() {
        return "hello world";
      }
      timeout().then(result => {
        console.log(result);
      });
      var res = timeout();
      console.log(res);

      console.log("虽然在后面，但是我先执行");

      function doubleAfter2seconds(num) {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve("111111");
          }, 2000);
        });
      }

      async function testResult() {
        let result = await doubleAfter2seconds(30);

        console.log(`调用的时候类似XXX.then(res=>{}); result就是resolve()里面的参数，也等价于res`);
        console.log(result);
      }

      testResult();




      function loadImg(src) {

        let promise = new Promise((resolve, reject) => {

          let img = new Image();
          img.src = src;
          img.onload = () => {
            resolve("加载成功");
          };
          img.onerror = () => {
            reject("加载失败")
          }
        });
        return promise;
      }

      let loadobj = loadImg("http://guzhoujianghaijiyusheng.cn/assets/img/markdownImg.6612cdc9.jpg");
      loadobj.then((res) => {
        console.log(res);

      })

      // 解构赋值

      var obj = {
        a: "aaaaa",
        b: "bbbbb",
        c: "ccccc"
      };

      // 对应key值要统一。就可以赋值。否则出现undefined
      var {
        a,
        c,
        b
      } = obj;

      console.log(a);
      console.log(c);
      console.log(b);

      var list = ["AAA", "BBB", "CCC"];

      // 对应顺序要统一
      var [A, B] = list;

      console.log(A);
      console.log(B);
      // console.log(C);


      // 箭头函数

      function fn() {
        var arr = [1, 2, 3];
        console.log(this);

        arr.map((item) => {
          console.log(item);
          console.log(this);
        })

        arr.map(function (item) {
          console.log(item);
          console.log(this);
        })
      }

      fn.call({
        a: "aaa"
      })

      console.group("for in 和 for of");
      let arr = [4,5,6];
      // for in 输出下标
      for (let key in arr) {
        console.log(key);
      }
      // for of 遍历数组
      for (let key of arr) {
        console.log(key);
      }

      // for in 输出下标
      for (let key in obj) {
        console.log(key);
      }
      // for of 遍历数组
      for (let key of obj) {
        console.log(key);
      }
      console.groupEnd()

    }
  };
</script>
<style scoped>
</style>
